home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Headers / g++ / SLList.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  3.8 KB  |  117 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988, 1992 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifndef _SLList_h
  20. #ifdef __GNUG__
  21. //#pragma interface
  22. #pragma cplusplus
  23. #endif
  24. #define _SLList_h 1
  25.  
  26. #include <Pix.h>
  27.  
  28. struct BaseSLNode
  29. {
  30.    BaseSLNode *tl;
  31.    void *item() {return (void*)(this+1);} // Return ((SLNode<T>*)this)->hd
  32. };
  33.  
  34. template<class T>
  35. class SLNode : public BaseSLNode
  36. {
  37.   public:
  38.     T                    hd; // Data part of node
  39.                          SLNode() { }
  40.                          SLNode(const T& h, SLNode* t = 0)
  41.                  : hd(h) { tl = t; }
  42.                          ~SLNode() { }
  43. };
  44.  
  45. extern int __SLListLength(BaseSLNode *ptr);
  46.  
  47. class BaseSLList {
  48.   protected:
  49.     BaseSLNode *last;
  50.     virtual void delete_node(BaseSLNode*node) = 0;
  51.     virtual BaseSLNode* copy_node(void* datum) = 0;
  52.     virtual void copy_item(void *dst, void *src) = 0;
  53.     virtual ~BaseSLList() { }
  54.     BaseSLList() { last = 0; }
  55.     void copy(const BaseSLList&);
  56.     BaseSLList& operator = (const BaseSLList& a);
  57.     Pix ins_after(Pix p, void *datum);
  58.     Pix prepend(void *datum);
  59.     Pix append(void *datum);
  60.     int remove_front(void *dst, int signal_error = 0);
  61.     void join(BaseSLList&);
  62.   public:
  63.     int length();
  64.     void clear();
  65.     Pix                   prepend(BaseSLNode*);
  66.     Pix                   append(BaseSLNode*);
  67.     int                   OK();
  68.     void                  error(const char* msg);
  69.     void                  del_after(Pix p);
  70.     int                   owns(Pix p);
  71.     void                  del_front();
  72. };
  73.  
  74. template <class T>
  75. class SLList : public BaseSLList
  76. {
  77.   private:
  78.     virtual void delete_node(BaseSLNode *node) { delete (SLNode<T>*)node; }
  79.     virtual BaseSLNode* copy_node(void *datum)
  80.     { return new SLNode<T>(*(T*)datum); }
  81.     virtual void copy_item(void *dst, void *src) { *(T*)dst = *(T*)src; }
  82.  
  83. public:
  84.     SLList() : BaseSLList() { }
  85.     SLList(const SLList<T>& a) : BaseSLList() { copy(a); }
  86.     SLList<T>&            operator = (const SLList<T>& a)
  87.     { BaseSLList::operator=((const BaseSLList&) a); return *this; }
  88.     virtual ~SLList() { clear(); }
  89.  
  90.     int                   empty() { return last == 0; }
  91.  
  92.     Pix prepend(T& item) {return BaseSLList::prepend(&item);}
  93.     Pix append(T& item) {return BaseSLList::append(&item);}
  94.     Pix prepend(SLNode<T>* node) {return BaseSLList::prepend(node);}
  95.     Pix append(SLNode<T>* node) {return BaseSLList::append(node);}
  96.  
  97.     T& operator () (Pix p) {
  98.     if (p == 0) error("null Pix");
  99.     return ((SLNode<T>*)(p))->hd; }
  100.     inline Pix first() { return (last == 0)? 0 : Pix(last->tl); }
  101.     void next(Pix& p)
  102.     { p = (p == 0 || p == last)? 0 : Pix(((SLNode<T>*)(p))->tl); }
  103.     Pix ins_after(Pix p, T& item) { return BaseSLList::ins_after(p, &item); }
  104.     void join(SLList<T>& a) { BaseSLList::join(a); }
  105.     
  106.     T& front() {
  107.     if (last == 0) error("front: empty list");
  108.     return ((SLNode<T>*)last->tl)->hd; }
  109.     T& rear() {
  110.     if (last == 0) error("rear: empty list");
  111.     return ((SLNode<T>*)last)->hd; }
  112.     int remove_front(T& x) { return BaseSLList::remove_front(&x); }
  113.     T remove_front() { T dst; BaseSLList::remove_front(&dst, 1); return dst; }
  114. };
  115.  
  116. #endif
  117.